<template>
  <basic-container>
    <div class="avue-crud">
      <el-form :inline="true">
        <el-form-item label="站点名称">
            <el-input v-model="name" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button v-if="permissions.syswebsite_add" icon="el-icon-search" type="primary"
                      @click="getList">
            查询
          </el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true">
        <el-form-item>
          <el-button v-if="permissions.syswebsite_get" icon="el-icon-plus" type="primary"
                     @click="addOrUpdateHandle(false)">
            添加
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        border
        v-loading="loading"
        :data="websiteList"
        row-key="id"
        lazy
        :load="treeLoad"
        :key="Math.random()"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
        <el-table-column type="index" label="编号"  width="60"></el-table-column>
        <el-table-column prop="name" label="站点名称" :show-overflow-tooltip="true" width="180"></el-table-column>
        <el-table-column prop="type" label="建站类型" align="center" width="100">
          <template slot-scope="scope">
            <el-tag  v-if="scope.row.type === 'ZD10001'">政府机关</el-tag>
            <el-tag  v-if="scope.row.type === 'ZD10002'">事业单位</el-tag>
            <el-tag  v-if="scope.row.type === 'ZD10003'">社会企业</el-tag>
            <el-tag  v-if="scope.row.type === 'ZD10004'">个人个体</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="path" label="所在城市" :show-overflow-tooltip="true">
          <template slot-scope="scope">
           <div>{{scope.row.provinceName}}{{scope.row.cityName}}-{{scope.row.areaName}}</div>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建日期" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sequence" label="排序" width="60"></el-table-column>
        <el-table-column prop="status" label="状态" width="80" align="center">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.status === 'ZT10001'">显示</el-tag>
            <el-tag type="warning" v-if="scope.row.status === 'ZT10002'">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-plus"
              @click="addOrUpdateHandle(false,scope.row.id)"
              v-if="permissions.syswebsite_add">添加
            </el-button>
            <el-button type="text"
                       icon="el-icon-edit"
                       @click="addOrUpdateHandle(true,scope.row.id)"
                       v-if="permissions.syswebsite_edit">修改
            </el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-if="permissions.syswebsite_del">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <table-form v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getList"></table-form>
    </div>
  </basic-container>
</template>

<script>
  import {fetchList, fetchWebsiteTree, getObj, addObj, putObj, delObj} from '@/api/sdAdmin/syswebsite'
  import TableForm from './website-form'
  import {mapGetters} from 'vuex'

  export default {
    name: "Website",
    components: {TableForm},
    data() {
      return {
        addOrUpdateVisible: false,
        // 遮罩层
        loading: true,
        // 菜单表格树数据
        websiteList: [],
        // 菜单树选项
        websiteOptions: [],
        name: null,
        maps: new Map()
      };
    },
    created() {
      this.getList();
    },
    computed: {
      ...mapGetters(['permissions']),
    },
    methods: {
      addOrUpdateHandle(isEdit, id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(isEdit, id)
        })
      },
      getList() {
        this.loading = true;
        fetchWebsiteTree(true,-1,this.name).then(response => {
          this.websiteList = response.data.data
          this.loading = false;
        });
      },
      handleDelete(row) {
        this.$confirm('是否确认删除名称为"' + row.name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          return delObj(row.id);
        }).then(() => {
          this.getList();
          const {parentId} = row  //取出当前删除行的pid
          const { tree, treeNode, resolve } = this.maps.get(parentId) //根据pid取出对应的节点数据
          // this.$set(this.$refs.table.store.states.lazyTreeNodeMap, parentId, []); //将对应节点下的数据清空，从而实现数据的重新加载
          this.treeLoad( tree, treeNode, resolve )
          this.$message.success('删除成功')
        })
      },
      treeLoad(tree, treeNode, resolve) {
        const pid = tree.id
        this.maps.set(pid,{ tree, treeNode, resolve })  //将当前选中节点数据存储到maps中
        fetchWebsiteTree(true,tree.id,this.name).then(response => {
          resolve(response.data.data)
        })
      }
    }
  };
</script>
